﻿define [
    'jquery'
    'use!underscore'
    'use!backbone'
    'libs/moment/moment.min'
], ($, _, Backbone) ->
  class Photo extends Backbone.Model
    constructor: (attributes, options) ->
      Backbone.Model.apply this, arguments

    initialize: ->
      @on 'change', () =>
        if @hasChanged() and @previousAttributes()['created'].match(/\/Date\(-?\d+\)\//)
          if typeof @get('cost') == 'string'
              @set 'cost', accounting.unformat(@get('cost'),',').toFixed(2).replace('.',','),
                silent: true
          @url = '/Photo/Edit/'
          @save @changedAttributes()

    defaults:
      name: 't'
      filename: 'blank.png'
      caption: 't'
      photographer: 't'
      licence: 't'
      cost: 0.0
      created: '/Date(' + moment().valueOf() + ')/'
      type: ''

    validate: (attrs) ->
      error = null
      _.each attrs, (val) ->
        error = 'Please compleate all fields'  if val is ''
      error  if error
